from wxpita import *
import wxpita.databind
from peak.events import trellis

ratings = ['Wretched', 'Bad', 'OK', 'Good', 'Excellent']

class Movie(trellis.Component):
    title = trellis.value('')
    rating = trellis.value(2)
    summary = trellis.value('')

    @trellis.action
    def show(self):
        print self
        print '-' * 79
        
    def __str__(self):
        s = self.summary
        s = (s[:100] + '...') if len(s) > 100 else s
        
        return '<%s, %s, "%s">' % (self.title, self.rating, s)

movie = Movie(title='Foo: The Movie', rating=4, summary='Everyone go foobar!')

f = SizedFrame [
    SizedPanel(sizerType='form') [
        StaticText(label='Title'),
        TextCtrl(name='title', expand=True),

        StaticText(label='Rating'),
        RadioBox(name='rating', choices=ratings),
        
        StaticText(label='Summary'),
        TextCtrl(name='summary', style='te_multiline', expand=True,
                 proportion=1, size=(-1, 200)),
    ],
    
    Button(name='clearBtn', label='Clear'),
]

@f.clearBtn.button_clicked
@trellis.modifier
def _(evt):
    movie.title = ''
    movie.rating = 2
    movie.summary = ''

@f.Init
def _():
    wxpita.databind.DataBind(movie, f, 'title, rating, summary')    

f.Show('Data Binding Example', size='fit')
